<?php 

require("../include/config.php");
require("../API/php/fonctions.php");
require("../API/php/header.php");

$modeDebug = false;

// phpsql/mysql_setStatutTicketsSupDeb.php?milis=123456789&ctrl=ok&id_toc=1245&revision=1&sn3=0&code_user=FRO

//[0][0] = responsable
//[0][1] = mail

// IN obligatoire
$arrayGet = array(
    "ctrl" => null,
    "id_toc" => null,
    "revision" => null,
    "sn3" => null,
    "code_user" => null
);

$arrayValeur = recupGet($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
	// CONNECION SQL
	$db = mysql_connect($host, $base, $mdp)  or die('Erreur de connexion '.mysql_error());
        mysql_query("SET NAMES 'utf8'");

	// CONNECION BASE
	mysql_select_db($base,$db)  or die('Erreur de selection '.mysql_error());
        
        $v_id = "";
        
        //cherche si présent
        $sql = "SELECT id
            FROM `".$prefixTable."tab_tickets_sup_deb`
            WHERE 1=1
            AND id_toc = '".$arrayValeur["id_toc"]."'
            AND revision = '".$arrayValeur["revision"]."'
	;";

	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
	
	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
            $v_id = $data['id']; 
        }
        
        if($v_id != ""){
            $sql = "UPDATE `".$prefixTable."tab_tickets_sup_deb` 
            SET `sn3` = ".$arrayValeur["sn3"]."
            ,`code_user` = '".$arrayValeur["code_user"]."'
            ,`date_modif` = NOW()
            WHERE 1=1
            AND id = ".$v_id."
            ;";
            $result = mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error()); 

            if($result == "1"){
                $strSorti .= "OK"
                .'&'
                ." \r\n";
            }
        }else{
            $sql = "INSERT INTO `".$prefixTable."tab_tickets_sup_deb` (
                    `id_toc` ,
                    `revision` ,
                    `sn3` ,
                    `code_user` ,
                    `date_creation`
                ) VALUES (
                    '".$arrayValeur["id_toc"]."',  ".$arrayValeur["revision"].",  ".$arrayValeur["sn3"].",  '".$arrayValeur["code_user"]."',  NOW()
                )
            ;";
            $result = mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error()); 

            if($result == "1"){
                $strSorti .= "OK"
                .'&'
                ." \r\n";
            }
        }
	
	
        
	// DECONNECION SQL
	mysql_close();
	
	if($modeDebug){
		$strSorti .= ('<br><br><br><br>'.$sql);
	}
}else{
	$strSorti = "ERROR:".$arrayValeur["error"];
}

require("../API/php/footer.php");
?>